package cn.edu.fzu.homemaking.service.impl;

import cn.edu.fzu.homemaking.api.OrderApi;
import cn.edu.fzu.homemaking.api.SsoApi;
import cn.edu.fzu.homemaking.common.PageVO;
import cn.edu.fzu.homemaking.domain.entity.OrderScore;
import cn.edu.fzu.homemaking.domain.enums.OrderStatusEnum;
import cn.edu.fzu.homemaking.domain.vo.OrderScoreVo;
import cn.edu.fzu.homemaking.domain.vo.OrderVo;
import cn.edu.fzu.homemaking.exception.ServiceException;
import cn.edu.fzu.homemaking.mapper.OrderScoreMapper;
import cn.edu.fzu.homemaking.service.OrderScoreService;
import cn.edu.fzu.homemaking.util.ResultUtil;
import cn.edu.fzu.homemaking.web.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author jiebin
 * @Date 2023/4/3
 */
@Service("orderScoreService")
public class OrderScoreServiceImpl implements OrderScoreService {

    @Resource
    private OrderScoreMapper orderScoreMapper;

    @Resource
    private OrderApi orderApi;

    @Resource
    private SsoApi ssoApi;

    @Override
    public void insert(String orderId, Long companyId, Long userId, Integer score, String comments) {
        orderScoreMapper.insert(orderId, companyId, userId, score, comments);
    }

    @Override
    public List<OrderScoreVo> list(Long companyId, Long userId, String startTime, String endTime) {
        if (companyId==null){
            throw new ServiceException("参数错误");
        }
        List<OrderScore> orderScores = orderScoreMapper.list(companyId, userId, startTime, endTime);
        List<OrderScoreVo> list = new ArrayList<>();
        for (OrderScore orderScore: orderScores) {
            OrderScoreVo orderScoreVo = new OrderScoreVo();
            BeanUtils.copyProperties(orderScore, orderScoreVo);
            Result res = ssoApi.getNameById(orderScore.getUserId(),"USER");
            String name = (String) res.getValue();
            orderScoreVo.setCustomerName(name);
            list.add(orderScoreVo);
        }
        return list;
    }

    @Override
    public OrderScoreVo selectByOrderId(String orderId) {
        OrderScoreVo orderScoreVo = new OrderScoreVo();
        OrderScore orderScore = orderScoreMapper.selectByOrderId(orderId);
        BeanUtils.copyProperties(orderScore, orderScoreVo);
        Result res = ssoApi.getNameById(orderScore.getUserId(),"USER");
        String name = (String) res.getValue();
        orderScoreVo.setCustomerName(name);
        return orderScoreVo;
    }
}
